package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
        public static final Short PASS_AUTH = 9;
    public static final Short FAIL_AUTH = 2;
    public static final Integer AUTHOR_TYPE = 2;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApAuthorFeign apAuthorFeign;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //判断参数
        if (dto==null){
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //初始化参数
        dto.checkParam();
        //查询条件
        QueryWrapper<ApUserRealname> queryWrapper = new QueryWrapper<>();
        if (dto.getStatus()!=null){
            queryWrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());
        }
        Page pageParam = new Page(dto.getPage(), dto.getSize());
        IPage page = page(pageParam, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setCode(0);
        responseResult.setData(page.getRecords());
        return (PageResponseResult)responseResult;
    }
    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto) {
        //检查参数
        if (dto==null ||dto.getId()==null || dto.getStatus()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if (dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);
        //认证通过添加作者和自媒体账号
        if (dto.getStatus().equals(PASS_AUTH)){
            ApUserRealname userRealname = getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
            if (apUser!=null){
                //检查自媒体用户是否存在不存在则新增
                WmUser wmUser = wmUserFeign.findByName(apUser.getName());
                if (wmUser==null){
                    wmUser=new WmUser();
                    wmUser.setApUserId(apUser.getId());
                    wmUser.setCreatedTime(new Date());
                    wmUser.setSalt(apUser.getSalt());
                    wmUser.setName(apUser.getName());
                    wmUser.setPassword(apUser.getPassword());
                    wmUser.setStatus(PASS_AUTH.intValue());
                    wmUser.setPhone(apUser.getPhone());
                    wmUserFeign.save(wmUser);
                }
                //检查作者账号是否存在
                ApAuthor apAuthor = apAuthorFeign.findByUserId(wmUser.getApUserId());
                if (apAuthor==null){
                    apAuthor = new ApAuthor();
                    apAuthor.setName(wmUser.getName());
                    apAuthor.setType(AUTHOR_TYPE);
                    apAuthor.setCreatedTime(new Date());
                    apAuthor.setUserId(wmUser.getApUserId());
                    apAuthorFeign.save(apAuthor);
                }
            }
            //修改ap_user标记
            apUser.setFlag((short)1);
            apUserMapper.updateById(apUser);
        }
//        int i= 1/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
